#include "CLAddThreadPool.h"

using namespace std;

CLAddThreadPool::CLAddThreadPool(int computeThreadNumber)
{
	this->computeThreadNumber = computeThreadNumber;
	this->computeThreadList.clear();
}

CLAddThreadPool::~CLAddThreadPool()
{
	this->computeThreadList.clear();
	delete this->userThread;
	delete this->masterThread;
}

void CLAddThreadPool::Run()
{
	this->userThread = new CLThreadForMsgLoop(new CLUserMsgProcessor, "user", true);
	this->userThread->Run(0);
	cout << "User thread running now..." << endl;
	
	this->masterThread = new CLThreadForMsgLoop(new CLMasterMsgProcessor, "master", true);
	this->masterThread->Run(0);
	cout << "Master thread running now..." << endl;
	
	sleep(1);
	cout << "Initialize compute threads" << endl;
	for(int i = 0; i < this->computeThreadNumber; i++)
	{
		stringstream ss;
		ss << "adder";
		ss << i;
		string adderName = ss.str();
		const char *name = adderName.c_str();
		
		CLThreadForMsgLoop *adder = new CLThreadForMsgLoop(new CLComputeMsgProcessor, name, true);
		CLExecutiveNameServer::PostExecutiveMessage("master", new CLRegisterMessage(name));
		adder->Run(0);
		
		this->computeThreadList.push_back(adder);
	}
}